package vip.mengqin.diandezhun.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.LifecycleOwner;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.ui.BaseFragment;
import com.android.baselibrary.util.GlideUtil;
import com.android.commcount.Lib_CommCount;
import com.android.commcount.bean.CommCount_Type;
import com.android.commcount.ui.activity.MyCountTypeListActivity;
import com.android.commcount.ui.fragment.CountHistoryFragment;
import com.gyf.immersionbar.ImmersionBar;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import vip.mengqin.diandezhun.R;
import vip.mengqin.diandezhun.adapter.MyBannerImageAdapter;
import vip.mengqin.diandezhun.bean.BannerInfo;
import vip.mengqin.diandezhun.event.Event_HomeTabCheck;
import vip.mengqin.diandezhun.manager.API_Manager;

public class HomeMainFragment extends BaseFragment {

    @BindView(R.id.ll_head)
    LinearLayout ll_head;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.framelayout)
    FrameLayout framelayout;
    @BindView(R.id.iv_start)
    ImageView iv_start;
    @BindView(R.id.tv_count_type)
    TextView tv_count_type;


    CommCount_Type currentCountType;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_main;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).titleBar(ll_head).statusBarDarkFont(true).navigationBarDarkIcon(true).init();

        getChildFragmentManager().beginTransaction().add(R.id.framelayout, new CountHistoryFragment()).commit();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mContext != null) {
            getBannerList();    //banner
            initCountType();
        }
    }

    //获取当前类型
    private void initCountType() {
        Lib_CommCount.getInstance().getType_Home(mContext, new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                currentCountType = (CommCount_Type) obj;
                GlideUtil.displayImage(mContext, currentCountType.thumb, iv_start, -1);
                tv_count_type.setText(currentCountType.title);
            }
        });
    }

    @OnClick({R.id.iv_start, R.id.ll_history_more, R.id.iv_changetype})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_start:   //点击拍照
                Lib_CommCount.startCount(mContext, currentCountType);
                break;
            case R.id.ll_history_more:  //更多记录
                Event_HomeTabCheck eventHomeTabCheck = new Event_HomeTabCheck();
                eventHomeTabCheck.index = 1;
                EventBus.getDefault().post(eventHomeTabCheck);
                break;
            case R.id.iv_changetype:    //切换类型
                Intent intent = new Intent(mContext, MyCountTypeListActivity.class);
                intent.putExtra("isShowSelect", true);
                startActivityForResult(intent, 100);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 200) {
            Lib_CommCount.getInstance().getType_Home(mContext, new CommCallBack() {
                @Override
                public void onResult(Object obj) {
                    currentCountType = (CommCount_Type) obj;
                    Lib_CommCount.startCount(mContext, currentCountType);
                }
            });
        }
    }

    //获取banner
    private void getBannerList() {
        API_Manager.getBannerList(mContext, new OkHttpCallBack<BaseResponce<List<BannerInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<List<BannerInfo>> baseResponce) {
                banner.setAdapter(new MyBannerImageAdapter(mContext, baseResponce.getData(), null))
                        .addBannerLifecycleObserver((LifecycleOwner) mContext)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(mContext));
                if (banner.getAdapter().getItemCount() == 0) {
                    banner.setVisibility(View.GONE);
                } else {
                    banner.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(BaseResponce<List<BannerInfo>> baseResponce) {
            }
        });
    }

}
